package com.cauli.manage.role.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cauli.manage.role.model.dto.AllocatedRoleQuery;
import com.cauli.manage.role.model.dto.RoleAddDTO;
import com.cauli.manage.role.model.vo.RoleVO;
import com.cauli.manage.role.model.dto.RoleQueryDTO;
import com.cauli.manage.role.model.dto.RoleUpdateDTO;
import com.cauli.manage.role.model.dto.RoleUpdateStatusDTO;
import com.cauli.manage.role.model.dto.UnallocatedRoleQuery;
import com.cauli.manage.role.model.entity.Role;
import com.cauli.manage.role.service.RoleService;
import com.cauli.manage.user.model.dto.UserVO;
import com.cauli.model.page.PageDTO;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2023/2/19 15:08
 * @description 角色 前端控制器
 */
@Api(tags = "角色")
@ApiSort(5)
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    public static final String CURRENT_MODULE = "角色";

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取角色列表")
    @GetMapping("/list")
    public ResultResponse<Role> listRole(RoleQueryDTO roleQueryDTO) {
        Page<Role> rolePage = roleService.getRolePage(roleQueryDTO);
        return ResultResponse.success().dataList(rolePage.getRecords(), rolePage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据角色ID获取详细信息")
    @GetMapping("/{roleId}")
    public ResultResponse<RoleVO> getRole(@PathVariable("roleId") Long roleId) {
        RoleVO roleVO = roleService.getRoleById(roleId);
        return ResultResponse.success().data(roleVO);
    }

    @MyLog(operation = "新增角色", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 3)
    @ApiOperation("新增角色")
    @PostMapping
    public ResultResponse<Void> addRole(@Valid @RequestBody RoleAddDTO roleAddDTO) {
        roleService.addRole(roleAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @MyLog(operation = "修改角色", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 4)
    @ApiOperation("修改角色")
    @PutMapping
    public ResultResponse<Void> updateRole(@Valid @RequestBody RoleUpdateDTO roleUpdateDTO) {
        roleService.updateRole(roleUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "角色状态修改", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 5)
    @ApiOperation("角色状态修改")
    @PutMapping("/status")
    public ResultResponse<Void> changeRoleStatus(@Valid @RequestBody RoleUpdateStatusDTO roleUpdateStatusDTO) {
        roleService.changeRoleStatus(roleUpdateStatusDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "删除角色", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 6)
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public ResultResponse<Void> removeRole(@PathVariable List<Long> roleIds) {
        roleService.deleteRole(roleIds);
        return ResultResponse.success().message("删除成功");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("查询已分配角色的用户列表")
    @GetMapping("/{roleId}/allocated/list")
    public ResultResponse<PageDTO<UserVO>> allocatedUserList(@PathVariable("roleId") Long roleId, AllocatedRoleQuery query) {
        query.setRoleId(roleId);
        PageDTO<UserVO> page = roleService.getAllocatedUserList(query);
        return ResultResponse.success().data(page);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("查询未分配角色的用户列表")
    @GetMapping("/{roleId}/unallocated/list")
    public ResultResponse<PageDTO<UserVO>> unallocatedUserList(@PathVariable("roleId") Long roleId, UnallocatedRoleQuery query) {
        query.setRoleId(roleId);
        PageDTO<UserVO> page = roleService.getUnallocatedUserList(query);
        return ResultResponse.success().data(page);
    }

    @MyLog(operation = "批量授权用户", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 9)
    @ApiOperation("批量授权用户")
    @PostMapping("/{roleId}/users/{userIds}/grant/bulk")
    public ResultResponse<Void> addRoleForUserByBulk(@PathVariable("roleId") Long roleId, @PathVariable("userIds") List<Long> userIds) {
        roleService.addRoleOfUserByBulk(roleId, userIds);
        return ResultResponse.success().message("授权成功");
    }

    @MyLog(operation = "批量取消授权用户", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 10)
    @ApiOperation("批量取消授权用户")
    @DeleteMapping("/users/{userIds}/grant/bulk")
    public ResultResponse<Void> deleteRoleOfUserByBulk(@PathVariable("userIds") List<Long> userIds) {
        roleService.deleteRoleOfUserByBulk(userIds);
        return ResultResponse.success().message("取消授权成功");
    }
}
